package com.xiaobao.campusLostFoundAfter.controller;

import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.pojo.Comment;
import com.xiaobao.campusLostFoundAfter.service.CommentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Comment")
public class CommentController {
    private final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    // 添加评论
    @PostMapping("add")
    public Result<String> addComment(@RequestBody Comment comment) {
        // 调用服务层方法添加评论
        int result = commentService.addComment(comment);
        // 根据添加结果返回成功或失败信息
        if (result == 1) {
            // 添加成功，返回成功信息
            return Result.success();
        } else {
            // 添加失败，返回错误信息
            return Result.error("添加失败");
        }
    }

    //查看评论单个物品的评论
    //带有物品id和物品类型
    @GetMapping("/getComments")
    public Result<List<Comment>> getComments(Integer ItemId, Integer type) {
        List<Comment> comments = commentService.getComments(ItemId, type);
        return Result.success(comments);
    }

    //删除自己的评论
    //
    @DeleteMapping("/delete")
    public Result<String> deleteComment(Integer id) {
        int result = commentService.deleteComment(id);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }
}
